<template>
  <el-form
    ref="formName"
    :model="rows || {}"
    :rules="rules"
    label-position="right"
    label-width="70px"
  >
    <el-form-item label="用户名" prop="username">
      <el-input v-model.trim="rows.username" disabled />
    </el-form-item>

    <el-form-item label="邮箱" prop="email">
      <el-input v-model.trim="rows.email" />
    </el-form-item>

    <el-form-item label="手机" prop="mobile">
      <el-input v-model.trim="rows.mobile" />
    </el-form-item>

    <el-form-item style="margin-bottom: -10px; margin-top: 40px">
      <el-row type="flex" justify="end">
        <el-button size="small" @click="resetForm">取消</el-button>
        <el-button type="primary" size="small" @click="submitForm">确定</el-button>
      </el-row>
    </el-form-item>
  </el-form>
</template>

<script>
import { usersModify } from '@/api/user'

export default {
  name: 'Modify',
  components: {},
  props: {
    rows: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      rules: {
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          {
            pattern: /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/,
            message: '请输入正确的邮箱地址',
            trigger: 'blur'
          }
        ],
        mobile: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          {
            pattern:
              /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/,
            message: '请输入正确的手机号',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {}, // 计算属性
  watch: {},
  created() {},
  mounted() {},
  methods: {
    submitForm() {
      this.$refs.formName.validate(async(valid) => {
        if (valid) {
          try {
            await usersModify({
              email: this.rows.email,
              mobile: this.rows.mobile
            }, this.rows.id)
            this.$emit('update:dashboardModify', false)
            this.$emit('users')
            this.resetForm()
          } catch (error) {
            this.$message.error('更新失败!')
            console.log(error)
          }
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.$refs.formName.resetFields()
      this.$emit('update:dashboardModify', false)
    }
  }
}
</script>

<style scoped>

</style>
